package com.yxt.admin.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxt.admin.entity.MenuEntity;
import com.yxt.admin.service.MenuService;
import com.yxt.admin.vo.MenuVO;
import com.yxt.common.utils.ajax.PageUtils;
import com.yxt.common.utils.ajax.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 菜单管理
 *
 * @author august
 * @email 379249906@qq.com
 * @date 2020-07-31 17:49:40
 */
@RestController
@RequestMapping("/user/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = menuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 查出所有分类以及子分类，以树形结构组装起来
     */
    @RequestMapping("/list/tree")
    public R list(){
        List<MenuVO> entities = menuService.listWithTree();
        return R.ok().put("data", entities);
    }

    /**
     * 通过menudID获取对应的权限
     */
    @RequestMapping("/permission")
    public R permission(String menuId){
        //list(Wrapper<T> queryWrapper);

        List<MenuEntity> perms = menuService.list(new QueryWrapper<MenuEntity>().
                eq("parentId", menuId).
                eq("type",2));

        return R.ok().put("data", perms);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{menuId}")
    public R info(@PathVariable("menuId") Long menuId){
		MenuEntity menu = menuService.getById(menuId);

        return R.ok().put("menu", menu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MenuEntity menu){
		menuService.save(menu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MenuEntity menu){
		menuService.updateById(menu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] menuIds){
		menuService.removeByIds(Arrays.asList(menuIds));

        return R.ok();
    }

}
